//
//  WPRefreshView.swift
//  WB
//
//  Created by 汪鹏 on 2017/1/16.
//  Copyright © 2017年 汪鹏. All rights reserved.
//

import UIKit

/*
 ios 系统中 UIView封装的旋转动画特点
 - 默认是顺时针旋转
 - 选择旋转方向时是根据最近原则（旋转的角度最小）
 
 */

class WPRefreshView: UIView {

    /// 指示器
    @IBOutlet weak var indicatiorView: UIActivityIndicatorView!
    
    /// 图标
    @IBOutlet weak var iconView: UIImageView!
    
    /// 描述文本
    @IBOutlet weak var descriptionTitle: UILabel!
    
    /// 刷新的状态
    var refreshState:refreshStatus = .normal {
        
        didSet{
            
            switch refreshState {
                
            case .normal:
                
                descriptionTitle.text = "下拉刷新数据..."
                
                indicatiorView.stopAnimating()
                
                iconView.isHidden = false
                
                UIView.animate(withDuration: 0.25, animations: { 
                    
                    self.iconView.transform = CGAffineTransform.identity
                })
                
            case .draging:
                
                descriptionTitle.text = "放手开始刷新..."
                
                UIView.animate(withDuration: 0.25, animations: { 
                    
                    self.iconView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI + 0.000001))
                })
                
            case .startRefresh:
                
                descriptionTitle.text = "数据正刷新中..."
                
                // 隐藏图标
                iconView.isHidden = true
                
                // 显示菊花
                indicatiorView.startAnimating()
            }
        
        }
    }
    
    // 类方法快速实例化刷新视图
    class func refreshView() ->WPRefreshView {
        
        let nib = UINib.init(nibName: "WPRefreshView", bundle: nil)
        
        let refreshView = nib.instantiate(withOwner: self, options: nil)[0] as! WPRefreshView
        
        return refreshView
    }

}
